package com.heer.gymnasium.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.heer.amap.utils.ToastUtil;
import com.heer.api.ResponseData.ApiResponse;
import com.heer.api.callback.BaseCallback;
import com.heer.gymnasium.AppHelper;
import com.heer.gymnasium.R;
import com.heer.gymnasium.util.ScreenUtils;

import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by heer on 17/5/15.
 */

public class SiteLevelDialog extends Dialog {

    private TextView btnSubmit;
    private ImageView imgClose;
    private RatingBar ratingLevel;

    private String qrString;
    private Context mContext;

    public SiteLevelDialog(Context context) {
        super(context);
        this.mContext = context;
        init(context);
    }

    public SiteLevelDialog(Context context, int themeResId) {
        super(context, themeResId);
        this.mContext = context;
        init(context);
    }

    protected SiteLevelDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.mContext = context;
        init(context);
    }

    public SiteLevelDialog(Context context, String qr) {
        super(context, R.style.dialog);
        this.qrString = qr;
        this.mContext = context;
        init(context);
    }

    private void init(final Context context) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE); //before
        this.setCanceledOnTouchOutside(true);
        setContentView(R.layout.dialog_site_level);

        /*
         * 将对话框的大小按屏幕大小的百分比设置
		 */
        Window dialogWindow = this.getWindow();
        WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        p.width = (int) (ScreenUtils.getScreenW() * 0.65); // 宽度设置为屏幕的0.65
        p.height = (int) (ScreenUtils.getScreenW() * 0.65); // 宽度设置为屏幕的0.65
        dialogWindow.setAttributes(p);

        imgClose = (ImageView) findViewById(R.id.btn_close);
        btnSubmit = (TextView) findViewById(R.id.btn_submit);
        ratingLevel = (RatingBar) findViewById(R.id.rating_level);
        imgClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        ratingLevel.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                // Since this rating bar is updated to reflect any of the other rating
                // bars, we should update it to the current values.
                if (ratingLevel.getRating() != rating) {
                    ratingLevel.setRating(rating);
                }
                final float ratingBarStepSize = ratingBar.getStepSize();
                if (ratingLevel.getStepSize() != ratingBarStepSize) {
                    ratingLevel.setStepSize(ratingBarStepSize);
                }
            }
        });

        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AppHelper.context().apiService().orderStar(qrString, (int) ratingLevel.getRating()).enqueue(new BaseCallback<ApiResponse>() {
                    @Override
                    public void onResponse(Call<ApiResponse> call, Response<ApiResponse> response) {
                        super.onResponse(call, response);
                        if (response.isSuccessful() && response.body().isSccessful())
                            ToastUtil.show(mContext, "评价成功");
                        else
                            ToastUtil.show(mContext, response.body().getMsg());
                        dismiss();
                    }

                    @Override
                    public void onFailure(Call<ApiResponse> call, Throwable t) {
                        super.onFailure(call, t);
                        dismiss();
                    }
                });
            }
        });
    }


}

